<?php
	namespace Admin\Controller;

	class CategoryController extends CommonController{

		public function Index(){

			$categoryModel=D('Category');
			$pid=$_GET['id']?$_GET['id']:0;
			$ppid=$_GET['ppid']=$_GET['ppid']?$_GET['ppid']:0;
			$like=$_GET['like'];
			if(empty($like))
			{
				$total=$categoryModel->where("pid=$pid")->count();
				$page= new \Think\Page($total,3);
				$page->setConfig('prev','上一页');
				$page->setConfig('next','下一页');

				$page_str = $page->show();

				$limit=$page->firstRow.','.$page->listRows;

				$list=$categoryModel->getAll($pid,$limit);
			}else{
				$where['name']=array('like','%'.$like.'%');
				$where['pid']=$pid;
				$total=$categoryModel->where($where)->count();
				$page= new \Think\Page($total,3);
				$page->setConfig('prev','上一页');
				$page->setConfig('next','下一页');
				$page_str = $page->show();
				$limit=$page->firstRow.','.$page->listRows;
				$list=$categoryModel->likeGet($where,$limit);
			}
			
			$this->assign('cateid',$pid);
			$this->assign('page',$page_str);
			$this->assign('ppid',$ppid);
			$this->assign('list',$list);
			$this->assign('like',$like);

			$this->display();

		} 
		
		public function Status(){
			$id=$_GET['id'];
			$status=$_GET['status'];
			$CategoryModel=D('Category');
			$res=$CategoryModel->change($status,$id);
			$this->redirect('Category/index');
		}
		
		public function Deleted(){
			$id=$_GET['id'];
			$CategoryModel=D('Category');
			$list=$CategoryModel->delFind($id);
			if(is_null($list)){
				$res=$CategoryModel->del($id);
				$this->success('删除成功',U('index'),2);
			}else{
				$this->error('存在子类不能删除啊!',U('index'));
			}
		}
		public function addcate(){
			$data['pid']=$_GET['cateid'];
			if(empty($_POST)){
				$this->assign('pid',$data['pid']);
				$this->display();
			}
			$CategoryModel=D('Category');
			$list['path']=$CategoryModel->addFind($data['pid']);
			$data['name']=$_POST['name'];
			$data['status']=$_POST['status'];
			$data['addtime']=time();
			$data['path']=$list['path'];
			$res=$CategoryModel->add1($data);
			if($res){
					$this->success('添加成功',U('index'),2);
				}else
				{
					$this->error('添加失败');
				}
		}
	}